home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / iritfltr / irit2plg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  11.7 KB  |  404 lines

  1. /*****************************************************************************
  2. * Filter to convert IRIT data files to REND386 (IBMPC only) Plg format.         *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Sep 1991    *
  5. *****************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9. #include <string.h>
  10. #include "irit_sm.h"
  11. #include "iritprsr.h"
  12. #include "allocate.h"
  13. #include "attribut.h"
  14. #include "iritgrap.h"
  15. #include "getarg.h"
  16. #include "genmat.h"
  17. #include "ffcnvrt.h"
  18. #include "ip_cnvrt.h"
  19.  
  20. #define GLOBAL_SCALE    200.0  /* Scale obj space -1..1 to -200..200 pixels. */
  21.  
  22. #ifdef NO_CONCAT_STR
  23. static char *VersionStr =
  24.     "Irit2Plg        Version 4.0,    Gershon Elber,\n\
  25.      (C) Copyright 1989/90/91/92/93 Gershon Elber, Non commercial use only.";
  26. #else
  27. static char *VersionStr = "Irit2Plg    " VERSION ",    Gershon Elber,    "
  28.     __DATE__ ",   " __TIME__ "\n" COPYRIGHT ", Non commercial use only.";
  29. #endif /* NO_CONCAT_STR */
  30.  
  31. static char
  32.     *CtrlStr = "irit2Plg l%- 4%- f%-FineNess!d T%- z%- DFiles!*s";
  33.  
  34. static int
  35.     GlblTalkative = FALSE,
  36.     GlblFineNess = 5,
  37.     FourPerFlat = FALSE;
  38.  
  39. static MatrixType CrntViewMat;            /* This is the current view! */
  40.  
  41. static int TransColorTable[][4] = {
  42.     { /* BLACK        */ 0,    0,   0,   0 },
  43.     { /* BLUE        */ 1,    0,   0, 255 },
  44.     { /* GREEN        */ 2,    0, 255,   0 },
  45.     { /* CYAN        */ 3,    0, 255, 255 },
  46.     { /* RED        */ 4,  255,   0,   0 },
  47.     { /* MAGENTA     */ 5,  255,   0, 255 },
  48.     { /* BROWN        */ 6,   50,   0,   0 },
  49.     { /* LIGHTGRAY    */ 7,  127, 127, 127 },
  50.     { /* DARKGRAY    */ 8,   63,  63,  63 },
  51.     { /* LIGHTBLUE    */ 9,    0,   0, 255 },
  52.     { /* LIGHTGREEN    */ 10,   0, 255,   0 },
  53.     { /* LIGHTCYAN    */ 11,   0, 255, 255 },
  54.     { /* LIGHTRED    */ 12, 255,   0,   0 },
  55.     { /* LIGHTMAGENTA    */ 13, 255,   0, 255 },
  56.     { /* YELLOW        */ 14, 255, 255,   0 },
  57.     { /* WHITE        */ 15, 255, 255, 255 },
  58.     { /* BROWN        */ 20,  50,   0,   0 },
  59.     { /* DARKGRAY    */ 56,  63,  63,  63 },
  60.     { /* LIGHTBLUE    */ 57,   0,   0, 255 },
  61.     { /* LIGHTGREEN    */ 58,   0, 255,   0 },
  62.     { /* LIGHTCYAN    */ 59,   0, 255, 255 },
  63.     { /* LIGHTRED    */ 60, 255,   0,   0 },
  64.     { /* LIGHTMAGENTA    */ 61, 255,   0, 255 },
  65.     { /* YELLOW        */ 62, 255, 255,   0 },
  66.     { /* WHITE        */ 63, 255, 255, 255 },
  67.     {               -1,   0,   0,   0 }
  68. };
  69.  
  70. static int PlgColorTable[16] = {
  71.     /* IRIT     PLG   */
  72.     15, /* BLOCK -> WHITE */
  73.     11, /* BLUE */
  74.     7,  /* GREEN */
  75.     8,  /* CYAN */
  76.     1,  /* RED */
  77.     13, /* MAGENTA */
  78.     2,  /* BROWN */
  79.     14, /* LIGHTGRAY */
  80.     14, /* DARKGRAY */
  81.     10, /* LIGHT BLUE */
  82.     9,  /* LIGHT GREEN */
  83.     8,  /* LIGHT CYAN */
  84.     1,  /* LIGHT RED */
  85.     13, /* LIGHT MAGENTA */
  86.     6,  /* YELLOW */
  87.     15, /* WHITE */    
  88. };
  89.  
  90. static void DumpDataForPlg(IPObjectStruct *PObjects);
  91. static void DumpOneObject(FILE *f, IPObjectStruct *PObject,
  92.                       int DumpVertices, int *IncVertices);
  93. static void DumpOnePolygon(FILE *f, IPPolygonStruct *PPolygon, int Color,
  94.                int DumpVertices, int *IncVertices, int IsPolygon);
  95. static RealType *MapPoint(RealType *Pt);
  96. static void Irit2PlgExit(int ExitCode);
  97.  
  98. /*****************************************************************************
  99. * Main routine - Read Parameter    line and do what you need...             *
  100. *****************************************************************************/
  101. void main(int argc, char **argv)
  102. {
  103.     int Error,
  104.     FineNessFlag = FALSE,
  105.     LinearOnePolyFlag = FALSE,
  106.     VerFlag = FALSE,
  107.     NumFiles = 0;
  108.     char
  109.     **FileNames = NULL;
  110.     IPObjectStruct *PObjects;
  111.  
  112.     if ((Error = GAGetArgs (argc, argv, CtrlStr, &LinearOnePolyFlag,
  113.                 &FourPerFlat, &FineNessFlag, &GlblFineNess,
  114.                 &GlblTalkative, &VerFlag, &NumFiles,
  115.                 &FileNames)) != 0) {
  116.     GAPrintErrMsg(Error);
  117.     GAPrintHowTo(CtrlStr);
  118.     Irit2PlgExit(1);
  119.     }
  120.  
  121.     if (VerFlag) {
  122.     fprintf(stderr, "\n%s\n\n", VersionStr);
  123.     GAPrintHowTo(CtrlStr);
  124.     Irit2PlgExit(0);
  125.     }
  126.  
  127.     if (LinearOnePolyFlag) {
  128.     fprintf(stderr, "Linear patch side will have a single polygon.\n");
  129.     CagdSetLinear2Poly(CAGD_ONE_POLY_PER_COLIN);
  130.     }
  131.     else
  132.         CagdSetLinear2Poly(CAGD_REG_POLY_PER_LIN);
  133.  
  134.     fprintf(stderr, "%s triangles per flat will be created.\n",
  135.         FourPerFlat ? "Four" : "Two");
  136.  
  137.     if (!NumFiles) {
  138.     fprintf(stderr, "No data file names were given, exit.\n");
  139.     GAPrintHowTo(CtrlStr);
  140.     Irit2PlgExit(1);
  141.     }
  142.  
  143.     /* Get the data files: */
  144.     if ((PObjects = IritPrsrGetDataFiles(FileNames, NumFiles, TRUE, FALSE)) ==
  145.                                     NULL)
  146.     Irit2PlgExit(1);
  147.  
  148.     if (IritPrsrWasPrspMat)
  149.         MatMultTwo4by4(CrntViewMat, IritPrsrViewMat, IritPrsrPrspMat);
  150.     else
  151.     GEN_COPY(CrntViewMat, IritPrsrViewMat, sizeof(MatrixType));
  152.  
  153.     DumpDataForPlg(PObjects);
  154.  
  155.     Irit2PlgExit(0);
  156. }
  157.  
  158. /*****************************************************************************
  159. * Routine to convert all surfaces/curves into polylines as follows:         *
  160. * Curves are converted to single polyline with SamplesPerCurve samples.         *
  161. * Surface are converted into GlblNumOfIsolines curves in each axes, each     *
  162. * handled as Curves above. The curves and surfaces are then deleted.         *
  163. *****************************************************************************/
  164. IPObjectStruct *IritPrsrProcessFreeForm(IPObjectStruct *CrvObjs,
  165.                     IPObjectStruct *SrfObjs)
  166. {
  167.     int LocalFourPerFlat;
  168.     float RelativeFineNess;
  169.     CagdCrvStruct *Crvs;
  170.     CagdSrfStruct *Srf, *Srfs;
  171.     IPObjectStruct *PObj, *PObjNext;
  172.     IPPolygonStruct *PPolygon, *PPolygonTemp;
  173.  
  174.     if (CrvObjs == NULL && SrfObjs == NULL)
  175.     return NULL;
  176.  
  177.     /* Make sure requested format is something reasonable. */
  178.     if (GlblFineNess < 2) {
  179.     GlblFineNess = 2;
  180.     fprintf(stderr, "FineNess is less than 2, 2 picked instead.\n");
  181.     }
  182.  
  183.     if (CrvObjs) {
  184.     /* Curves are not rendered at this time and they are ignored. */
  185.     for (PObj = CrvObjs; PObj != NULL;) {
  186.         if (GlblTalkative)
  187.         fprintf(stderr, "Processing curve object \"%s\"\n",
  188.             PObj -> Name);
  189.  
  190.         Crvs = PObj -> U.Crvs;
  191.         CagdCrvFreeList(Crvs);
  192.         PObjNext = PObj -> Pnext;
  193.         PObj -> U.Crvs = NULL;
  194.         IPFreeObject(PObj);
  195.         PObj = PObjNext;
  196.     }
  197.     CrvObjs = NULL;
  198.     }
  199.  
  200.     if (SrfObjs) {
  201.     for (PObj = SrfObjs; PObj != NULL; PObj = PObj -> Pnext) {
  202.         if (GlblTalkative)
  203.         fprintf(stderr, "Processing surface object \"%s\"\n",
  204.             PObj -> Name);
  205.  
  206.         Srfs = PObj -> U.Srfs;
  207.         PObj -> U.Pl = NULL;
  208.         PObj -> ObjType = IP_OBJ_POLY;
  209.         IP_SET_POLYGON_OBJ(PObj);
  210.  
  211.         LocalFourPerFlat = FourPerFlat;
  212.  
  213.         if (AttrGetObjectStrAttrib(PObj, "twoperflat"))
  214.         LocalFourPerFlat = FALSE;
  215.         if (AttrGetObjectStrAttrib(PObj, "fourperflat"))
  216.         LocalFourPerFlat = TRUE;
  217.  
  218.         if ((RelativeFineNess = AttrGetObjectRealAttrib(PObj,
  219.                      "resolution")) > IP_ATTR_BAD_REAL)
  220.         RelativeFineNess = 1.0;
  221.  
  222.         for (Srf = Srfs; Srf != NULL; Srf = Srf -> Pnext) {
  223.         PPolygon = PPolygonTemp =
  224.             IritSurface2Polygons(Srf, LocalFourPerFlat,
  225.                   (int) (RelativeFineNess * GlblFineNess), FALSE);
  226.         while (PPolygonTemp -> Pnext)
  227.             PPolygonTemp = PPolygonTemp -> Pnext;
  228.         PPolygonTemp -> Pnext = PObj -> U.Pl;
  229.         PObj -> U.Pl = PPolygon;
  230.         }
  231.         CagdSrfFreeList(Srfs);
  232.     }
  233.     }
  234.  
  235.     return SrfObjs;
  236. }
  237.  
  238. /*****************************************************************************
  239. * Dumps the data for REND386 Plg into stdout.                     *
  240. *****************************************************************************/
  241. static void DumpDataForPlg(IPObjectStruct *PObjects)
  242. {
  243.     int IncVertices,
  244.         TotalVertices = 0,
  245.     TotalPolys = 0;
  246.     char
  247.     *Name = NULL;
  248.     IPVertexStruct *PVertex;
  249.     IPPolygonStruct *PPoly;
  250.     IPObjectStruct *PObj,
  251.     *PObjHead = NULL;
  252.     FILE *f = stdout;           /* Change it if you want it into "real" file. */
  253.     
  254.     /* Reverse object list since it was loaded in reverse by iritprsr module.*/
  255.     while (PObjects != NULL) {
  256.     PObj = PObjects;
  257.     PObjects = PObjects -> Pnext;
  258.     PObj -> Pnext = PObjHead;
  259.     PObjHead = PObj;
  260.     }
  261.     PObjects = PObjHead;
  262.  
  263.     /* Count how many polygons/vertices we have in this data set and print. */
  264.     for (PObj = PObjects; PObj != NULL; PObj = PObj -> Pnext) {
  265.     if (!IP_IS_POLY_OBJ(PObj) || !IP_IS_POLYGON_OBJ(PObj))
  266.         continue;
  267.  
  268.     if (Name == NULL && strlen(PObj -> Name) > 0)
  269.         Name = PObj -> Name;
  270.  
  271.     for (PPoly = PObj -> U.Pl;
  272.          PPoly != NULL;
  273.          PPoly = PPoly -> Pnext) {
  274.         TotalPolys++;
  275.  
  276.         for (PVertex = PPoly -> PVertex;
  277.          PVertex != NULL;
  278.          PVertex = PVertex -> Pnext) {
  279.         TotalVertices++;
  280.         }
  281.     }
  282.     }
  283.  
  284.     if (Name != NULL)
  285.         fprintf(f, "IRIT %d %d\n", TotalVertices, TotalPolys);
  286.     else
  287.     fprintf(f, "%s_IRIT %d %d\n", Name, TotalVertices, TotalPolys);
  288.  
  289.     for (IncVertices = 0, PObj = PObjects; PObj != NULL; PObj = PObj -> Pnext)
  290.     DumpOneObject(f, PObj, TRUE, &IncVertices);
  291.     if (IncVertices != TotalVertices) {
  292.     fprintf(stderr, "Inconsistent vertices count.\n");
  293.     Irit2PlgExit(1);
  294.     }
  295.  
  296.     for (IncVertices = 0, PObj = PObjects; PObj != NULL; PObj = PObj -> Pnext)
  297.     DumpOneObject(f, PObj, FALSE, &IncVertices);
  298.     if (IncVertices != TotalVertices) {
  299.     fprintf(stderr, "Inconsistent vertices count.\n");
  300.     Irit2PlgExit(1);
  301.     }
  302.  
  303.     fclose(f);
  304. }
  305.  
  306. /*****************************************************************************
  307. * Routine to dump one object PObject.                         *
  308. *****************************************************************************/
  309. static void DumpOneObject(FILE *f, IPObjectStruct *PObject,
  310.                       int DumpVertices, int *IncVertices)
  311. {
  312.     int i, Color;
  313.     IPPolygonStruct *PList;
  314.  
  315.     if (!IP_IS_POLY_OBJ(PObject))
  316.     return;
  317.  
  318.     PList = PObject -> U.Pl;
  319.  
  320.     if ((Color = AttrGetObjectColor(PObject)) != IP_ATTR_NO_COLOR) {
  321.     for (i = 0; TransColorTable[i][0] >= 0; i++) {
  322.         if (TransColorTable[i][0] == Color) {
  323.         Color = PlgColorTable[i];
  324.         break;
  325.         }
  326.     }
  327.     }
  328.     else
  329.         Color = IG_DEFAULT_COLOR;
  330.  
  331.     while (PList) {
  332.     DumpOnePolygon(f, PList, Color, DumpVertices, IncVertices,
  333.                         IP_IS_POLYGON_OBJ(PObject));
  334.     PList =    PList -> Pnext;
  335.     }
  336. }
  337.  
  338. /*****************************************************************************
  339. * Routine to dump one polygon, using global Matrix transform CrntViewMat.    *
  340. *****************************************************************************/
  341. static void DumpOnePolygon(FILE *f, IPPolygonStruct *PPolygon, int Color,
  342.                int DumpVertices, int *IncVertices, int IsPolygon)
  343. {
  344.     int CountVertices;
  345.     RealType *MappedPoint;
  346.     IPVertexStruct *V,
  347.     *VList = PPolygon -> PVertex;
  348.  
  349.     if (VList == NULL)
  350.     return;
  351.  
  352.     if (DumpVertices && !IritPrsrIsConvexPolygon(PPolygon)) {
  353.     static int
  354.         Printed = FALSE;
  355.  
  356.     if (!Printed) {
  357.         fprintf(stderr,
  358.             "\nWARNING: Non convex polygon(s) might be in data (see CONVEX in IRIT),\n\t\t\t\toutput can be wrong as the result!\n");
  359.         Printed = TRUE;
  360.     }
  361.     }
  362.  
  363.     if (IsPolygon) {
  364.     if (DumpVertices) {
  365.         for (V = VList; V != NULL; V = V -> Pnext) {
  366.         MappedPoint = MapPoint(V -> Coord);
  367.         fprintf(f, "%4d %4d %4d\n",
  368.             (int) (MappedPoint[0] * GLOBAL_SCALE),
  369.             (int) (MappedPoint[1] * GLOBAL_SCALE),
  370.             (int) (MappedPoint[2] * GLOBAL_SCALE));
  371.         (*IncVertices)++;
  372.         }
  373.     }
  374.     else {
  375.         for (CountVertices = 0, V = VList; V != NULL; V = V -> Pnext)
  376.             CountVertices++;
  377.         fprintf(f, "0x%02xff %d", Color | 0x0010, CountVertices);
  378.         while (CountVertices-- > 0)
  379.             fprintf(f, " %d", (*IncVertices)++);
  380.         fprintf(f, "\n");
  381.     }
  382.     }
  383. }
  384.  
  385. /*****************************************************************************
  386. * Maps the given E3 point using the CrntViewMat.                 *
  387. *****************************************************************************/
  388. static RealType *MapPoint(RealType *Pt)
  389. {
  390.     static RealType MappedPt[3];
  391.  
  392.     MatMultVecby4by4(MappedPt, Pt, CrntViewMat);
  393.  
  394.     return MappedPt;
  395. }
  396.  
  397. /*****************************************************************************
  398. * Irit2Plg exit routine.                             *
  399. *****************************************************************************/
  400. static void Irit2PlgExit(int ExitCode)
  401. {
  402.     exit(ExitCode);
  403. }
  404.